package com.biShe.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.biShe.entity.BuyedGoods;
import com.biShe.entity.Category;
import com.biShe.entity.OrdersCBuyedGoods;
import com.biShe.mapper.BuyedGoodsMapper;
import com.biShe.mapper.CategoryMapper;
import com.biShe.mapper.OrdersCBuyedGoodsMapper;
import com.biShe.utils.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 被购单品详细记录表,从购物车传来并保存 前端控制器
 * </p>
 *
 * @author 小刘
 * @since 2023-05-10
 */
@Configuration
@RestController
@RequestMapping("/buyed-goods")
public class BuyedGoodsController {
    @Autowired
    private OrdersCBuyedGoodsMapper ordersCBuyedGoodsMapper;

    @Autowired
    private BuyedGoodsMapper categoryMapper;

    /**
     * 查找所有已售商品
     *
     * @return 返回所有已售商品信息列表
     */
    @ApiOperation(value="获取所有已售商品信息", notes="返回所有已售商品列表")
    @GetMapping("/list/{page}/{size}")
    public Result list(@PathVariable("page") String page, @PathVariable("size") String size) {
        Page pageHelper = PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(size));
        List<BuyedGoods> categoryList = categoryMapper.selectList(null);
        PageInfo pageInfo = new PageInfo(categoryList);
        return Result.succ(pageInfo);
    }

    /**
     * 根据订单id查询具体的购买商品信息，需要用一个中间表
     *
     * @return 返回所有已售商品信息列表
     */
    @ApiOperation(value="获取所有已售商品信息", notes="返回所有已售商品列表")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Int", paramType = "path")
    @GetMapping("{id}")
    public Result list(@PathVariable("id") Integer id) {
        List<BuyedGoods> buyedGoods = new ArrayList<>();
        QueryWrapper<OrdersCBuyedGoods> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("orders_id",id);
        List<OrdersCBuyedGoods> mids = ordersCBuyedGoodsMapper.selectList(queryWrapper1);
        //通过中间表获取被买商品的id，用来查找详细信息
        for (OrdersCBuyedGoods item :mids){
            BuyedGoods buyedGoods1 = new BuyedGoods();
            buyedGoods1 = categoryMapper.selectById(item.getBuyedGoodsId());
            buyedGoods.add(buyedGoods1);
        }

        return Result.succ(buyedGoods);
    }

}

